package com.py.od.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;

import com.py.od.domain.*;
import com.ruoyi.common.security.utils.SecurityUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import com.py.api.PyCodeApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.py.od.service.IOdInventoryTransferService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
/**
 * 库存调拨Controller
 * 
 * @author py
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/transfer")
public class OdInventoryTransferController extends BaseController
{
    @Autowired
    private IOdInventoryTransferService odInventoryTransferService;


    @Resource
    private PyCodeApi pyCodeApi;

    /**
     *  获取编码
     * @param code
     * @return
     */

    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code",required = false) Integer code){
        System.out.println("进来了"+code);
        return pyCodeApi.generateCode(code);
    }


    /**
     * 查询库存调拨列表
     */
    @RequiresPermissions("transfer:transfer:list")
    @GetMapping("/list")
    public TableDataInfo list(OdInventoryTransfer odInventoryTransfer)
    {
        startPage();
        //仓库表为空
//        System.out.println(odInventoryTransfer.getOdWarehouseList());
        List<OdInventoryTransfer> list = odInventoryTransferService.selectOdInventoryTransferList(odInventoryTransfer);
//        System.out.println("afaf:"+list);
        return getDataTable(list);
    }

    /**
     * 导出库存调拨列表
     */
    @RequiresPermissions("transfer:transfer:export")
    @Log(title = "库存调拨", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OdInventoryTransfer odInventoryTransfer)
    {
        List<OdInventoryTransfer> list = odInventoryTransferService.selectOdInventoryTransferList(odInventoryTransfer);
        ExcelUtil<OdInventoryTransfer> util = new ExcelUtil<OdInventoryTransfer>(OdInventoryTransfer.class);
        util.exportExcel(response, list, "库存调拨数据");
    }

    /**
     * 获取库存调拨详细信息
     */
    @RequiresPermissions("transfer:transfer:query")
    @GetMapping(value = "/{inventoryTransferId}")
    public AjaxResult getInfo(@PathVariable("inventoryTransferId") Long inventoryTransferId)
    {
        return success(odInventoryTransferService.selectOdInventoryTransferByInventoryTransferId(inventoryTransferId));
    }



    /**
     * 新增库存调拨
     */
    @RequiresPermissions("transfer:transfer:add")
    @Log(title = "库存调拨", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OdInventoryTransfer odInventoryTransfer)
    {
        System.out.println("number:"+odInventoryTransfer.getNumber());
        System.out.println("codeId:"+odInventoryTransfer.getCodeId());
        pyCodeApi.saveCode(odInventoryTransfer.getCodeId(),odInventoryTransfer.getNumber());
        odInventoryTransfer.setStatus("0");
        odInventoryTransfer.setCreator(SecurityUtils.getUsername());
        SysUser sysUser =odInventoryTransferService.selectBySysUserName(SecurityUtils.getUsername());
        odInventoryTransfer.setDepartmentId(sysUser.getDeptName());
        odInventoryTransfer.setSourceStatus("0");
        odInventoryTransfer.setDestinationStatus("0");
        odInventoryTransfer.setIsDeleted(0);
        System.out.println("调拨表:"+odInventoryTransfer);
        return toAjax(odInventoryTransferService.insertOdInventoryTransfer(odInventoryTransfer));
    }

    /**
     * 修改库存调拨
     */
    @RequiresPermissions("transfer:transfer:edit")
    @Log(title = "库存调拨", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OdInventoryTransfer odInventoryTransfer)

    {
        System.out.println("odIn:"+odInventoryTransfer);
        return toAjax(odInventoryTransferService.updateOdInventoryTransfer(odInventoryTransfer));
    }

    /**
     * 修改单据状态
     */
    @RequiresPermissions("transfer:transfer:edit")
    @Log(title = "库存调拨", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult status(@RequestBody OdInventoryTransfer odInventoryTransfer )
    {
        odInventoryTransfer.setSourceStatus("4");
        odInventoryTransfer.setDestinationStatus("1");
        odInventoryTransfer.setReviewTime(new Date());
        odInventoryTransfer.setReviewer(SecurityUtils.getUsername());
        OdTransferDetails odTransferDetails=new OdTransferDetails();
        odTransferDetails.setTransferNumber(odInventoryTransfer.getTransferNumber());
        List<OdTransferDetails> list=odInventoryTransferService.selectByNumberList(odTransferDetails);
        list.forEach(l->{
            WhInventory whInventory=new WhInventory();
            OdWarehouse odWarehouse= odInventoryTransferService.selectOdWarehouse(Math.toIntExact(odInventoryTransfer.getInboundWarehouseId()));
//            whInventory.setWarehouseCode(odWarehouse.getWarehouseCode());
            whInventory.setLocationCode(l.getDlLocationCode());
            whInventory.setProductCode(l.getProductCode());
            WhInventory W= odInventoryTransferService.seleWhInventory(whInventory);
            if(W==null){
                WhInventory ww=new WhInventory();
                ww.setProductCode(l.getProductCode());
                ww.setWarehouseCode(odWarehouse.getWarehouseCode());
                ww.setCurrentInventory((long) l.getTransferQuantity());
                ww.setLocationCode(l.getDlLocationCode());
                ww.setInventoryValue(l.getInboundPrice().multiply(BigDecimal.valueOf(l.getTransferQuantity())));
                ww.setPendingIn((long) l.getTransferQuantity());
                ww.setPendingOut(0L);
                ww.setAvailableInventoryValue(l.getInboundPrice().multiply(BigDecimal.valueOf(l.getTransferQuantity())));
                ww.setInventoryLowerLimit(100L);
                ww.setInventoryUpperLimit(100L);
                System.out.println("wwww:"+ww);
                odInventoryTransferService.WhinaddAll(ww);

                System.out.println("----------调出---------------");
                WhInventory whInventory10=new WhInventory();
//                whInventory1.setWarehouseCode(odWarehouse1.getWarehouseCode());
                whInventory10.setLocationCode(l.getDcLocationCode());
                whInventory10.setProductCode(l.getProductCode());
//                OdWarehouse odWarehouse1= odInventoryTransferService.selectOdWarehouse(Math.toIntExact(odInventoryTransfer.getInboundWarehouseId()));
                WhInventory Xoo= odInventoryTransferService.seleWhInventory(whInventory10);
                WhInventory whInventory20=new WhInventory();
                whInventory20.setLocationCode(l.getDcLocationCode());
                whInventory20.setProductCode(l.getProductCode());
                System.out.println("adada:"+(Xoo.getCurrentInventory()-l.getTransferQuantity()));
                whInventory20.setCurrentInventory(Xoo.getCurrentInventory()-l.getTransferQuantity());
                odInventoryTransferService.DcWhinupdate(whInventory20);
                  if( odInventoryTransferService.DcWhinupdate(whInventory20)>0){
                      System.out.println("修改成功！！！！");
                  }else{
                      System.out.println("修改失败！！！");
                  }
            }else {
                System.out.println("----------调出---------------");
                WhInventory whInventory1=new WhInventory();
//                whInventory1.setWarehouseCode(odWarehouse1.getWarehouseCode());
                whInventory1.setLocationCode(l.getDcLocationCode());
                whInventory1.setProductCode(l.getProductCode());
//                OdWarehouse odWarehouse1= odInventoryTransferService.selectOdWarehouse(Math.toIntExact(odInventoryTransfer.getInboundWarehouseId()));
                WhInventory Wtow= odInventoryTransferService.seleWhInventory(whInventory1);
                WhInventory whInventory2=new WhInventory();
                whInventory2.setLocationCode(l.getDcLocationCode());
                whInventory2.setProductCode(l.getProductCode());
                System.out.println("adada:"+(Wtow.getCurrentInventory()-l.getTransferQuantity()));
                whInventory2.setCurrentInventory(Wtow.getCurrentInventory()-l.getTransferQuantity());
                odInventoryTransferService.DcWhinupdate(whInventory2);
                  if( odInventoryTransferService.DcWhinupdate(whInventory2)>0){
                      System.out.println("修改成功！！！！");
                  }else{
                      System.out.println("修改失败！！！");
                  }
                System.out.println("-------------调入--------------");
                WhInventory whI1=new WhInventory();
//                whInventory1.setWarehouseCode(odWarehouse1.getWarehouseCode());
                whI1.setLocationCode(l.getDlLocationCode());
                whI1.setProductCode(l.getProductCode());
                OdWarehouse odw1= odInventoryTransferService.selectOdWarehouse(Math.toIntExact(odInventoryTransfer.getInboundWarehouseId()));
                WhInventory Wtow1= odInventoryTransferService.seleWhInventory(whI1);
                WhInventory whIn2=new WhInventory();
                whIn2.setLocationCode(l.getDlLocationCode());
                whIn2.setProductCode(l.getProductCode());
                System.out.println("getDlLocationCode:"+l.getDlLocationCode());
                System.out.println("getProductCode:"+l.getProductCode());
                System.out.println("adada:"+(Wtow1.getCurrentInventory()));
                whIn2.setCurrentInventory(Wtow1.getCurrentInventory()+l.getTransferQuantity());
                odInventoryTransferService.DlWhinuppdate(whIn2);
                if( odInventoryTransferService.DlWhinuppdate(whIn2)>0){
                    System.out.println("修改成功！！！！");
                }else{
                    System.out.println("修改失败！！！");
                }
            }
        });


//        odInventoryTransfer.setWhInventoryList(odInventoryTransferService.seleWhInventoryResult(whInventory));
//        System.out.println("sADAD:"+odInventoryTransferService.seleWhInventoryResult(whInventory));
//        if (odInventoryTransferService.seleWhInventoryResult(whInventory)!=null){
//            System.out.println("-----查询到值-----");
//            System.out.println("调出："+ odInventoryTransferService.DcWhinupdate(whInventory));
//            odInventoryTransferService.DcWhinupdate(whInventory);
//            System.out.println("调入：" +  odInventoryTransferService.DcWhinupdate(whInventory));
//                 odInventoryTransferService.DlWhinuppdate(whInventory);
//        }else{
//            System.out.println("-----查询没值----");
//            odInventoryTransferService.WhinaddAll(whInventory);
//        }
        return    toAjax(odInventoryTransferService.updateStatus(odInventoryTransfer));

    }



    /**
     * 删除库存调拨
     */
    @RequiresPermissions("transfer:transfer:remove")
    @Log(title = "库存调拨", businessType = BusinessType.DELETE)
	@DeleteMapping("/{inventoryTransferId}")
    public AjaxResult remove(@PathVariable Long[] inventoryTransferId)
    {
        System.out.println(inventoryTransferId);
        return toAjax(odInventoryTransferService.deleteOdInventoryTransferByInventoryTransferIds(inventoryTransferId));
    }

    //查询详情
    @GetMapping("/selectOdInventoryTransfer")
    public OdInventoryTransfer selectOdInventoryTransfer(@RequestParam("inventoryTransferId")Long inventoryTransferId){
//        System.out.println(odInventoryTransferService.selectOdInventoryTransferByInventoryTransferId(inventoryTransferId));
        System.out.println("-----进来了3------");
        return odInventoryTransferService.selectOdInventoryTransferByInventoryTransferId(inventoryTransferId);
    }


    @GetMapping("/selectWhInventoryByCode")
    public AjaxResult selectWhInventoryByCode(@RequestParam("location") String location,
                                              @RequestParam("productCode") String productCode) {

        WhInventory whInventory = new WhInventory();
        whInventory.setLocationCode(location);
        whInventory.setProductCode(productCode);
        List<WhInventory> whInventories = odInventoryTransferService.selectWhInventoryByCode(whInventory);
        System.out.println("集合:" + whInventories);
        return success(whInventories);

    }

    @GetMapping("/selectAllBsWhLocation")
    public AjaxResult selectAllBsWhLocation(@RequestParam("warehouseId") Integer warehouseId) {

        System.out.println("仓库id:" + warehouseId);
        return success(odInventoryTransferService.selectAllBsWhLocation(warehouseId));

    }
}
